﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace TicTacToe.Core.AI
{
    static partial class Conditions
    {
        static readonly Random rand = new Random();
        static readonly Dictionary<Func<AIGameField, int, int, int, int, ConditionResult>, AIDifficultyLevel> difficulties =
            new Dictionary<Func<AIGameField, int, int, int, int, ConditionResult>, AIDifficultyLevel>
            {
                {InnerCube, AIDifficultyLevel.NormalUp}, {Random, AIDifficultyLevel.Easy|AIDifficultyLevel.Normal},
                {CornerField, AIDifficultyLevel.NormalUp}, {MiddleField, AIDifficultyLevel.NormalUp},
                {NextToOwnField, AIDifficultyLevel.Easy|AIDifficultyLevel.NormalUp}, {OwnXThreeRow, AIDifficultyLevel.All}, 
                {OwnXYThreeRow, AIDifficultyLevel.All}, {OwnXZThreeRow, AIDifficultyLevel.All},
                {OwnYThreeRow, AIDifficultyLevel.All}, {OwnZThreeRow, AIDifficultyLevel.All}, 
                {OwnYZThreeRow, AIDifficultyLevel.All}, {OwnXYZThreeRow, AIDifficultyLevel.All},
                {OwnXTwoRow, AIDifficultyLevel.All}, {OwnYTwoRow, AIDifficultyLevel.All},
                {OwnXYTwoRow, AIDifficultyLevel.All}, {OwnZTwoRow, AIDifficultyLevel.NormalUp},
                {OwnXZTwoRow, AIDifficultyLevel.NormalUp}, {OwnYZTwoRow, AIDifficultyLevel.NormalUp},
                {OwnXTwoAndOneRow, AIDifficultyLevel.All}, {OwnXYTwoAndOneRow, AIDifficultyLevel.All},
                {OwnYTwoAndOneRow, AIDifficultyLevel.All}, {OwnZTwoAndOneRow, AIDifficultyLevel.All},
                {EnemyXThreeRow, AIDifficultyLevel.All}, {EnemyXYThreeRow, AIDifficultyLevel.All},
                {EnemyYThreeRow, AIDifficultyLevel.All}, {EnemyZThreeRow, AIDifficultyLevel.All},
                {EnemyXYZThreeRow, AIDifficultyLevel.All}, {EnemyXZThreeRow, AIDifficultyLevel.All},
                {EnemyYZThreeRow, AIDifficultyLevel.All}, {EnemyXTwoRow, AIDifficultyLevel.All},
                {EnemyXYTwoRow, AIDifficultyLevel.All}, {EnemyYTwoRow, AIDifficultyLevel.All},
                {EnemyZTwoRow, AIDifficultyLevel.NormalUp}, {EnemyXZTwoRow, AIDifficultyLevel.NormalUp},
                {EnemyYZTwoRow, AIDifficultyLevel.NormalUp}, {EnemyXTwoAndOneRow, AIDifficultyLevel.All},
                {EnemyYTwoAndOneRow, AIDifficultyLevel.All}, {EnemyZTwoAndOneRow, AIDifficultyLevel.All},
                {EnemyXYTwoAndOneRow, AIDifficultyLevel.All},
            };

        #region API Methods

        public static int GetScore(ConditionResult result)
        {
            return (int)result;
        }

        public static IEnumerable<Func<AIGameField,int,int,int,int,ConditionResult>> GetNewRuleset(AIDifficultyLevel level)
        {
            return difficulties.Keys.Where(n => difficulties[n].HasFlag(level));
        }

        #endregion
    }
}
